<style>
.app-main{
  background: #f5f9ff;
}
</style>
<template>
  <div class="app-container" style="margin: auto; margin-top: 20px; border-radius:5px; width: 1300px; background: #FFFFFF">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择日期">
        <el-date-picker
          v-model="daterangeTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div id="map" style="width: 1300px; height: 400px;"></div>

    <h3>订单列表</h3>
    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="ddh" />
      <el-table-column label="始发地" align="center" prop="sfd" />
      <el-table-column label="目的地" align="center" prop="mdd" />
      <el-table-column label="客户名称" align="center" prop="kcname" />
      <el-table-column label="订单状态" align="center" prop="status">
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag v-if="scope.row.status==0" type="info">待调度</el-tag>-->
        <!--            <el-tag v-else-if="scope.row.status==1" type="success">已调度</el-tag>-->
        <!--          </template>-->
      </el-table-column>
      <el-table-column label="货物数量" align="center" prop="hwnum" />
      <el-table-column label="总体积" align="center" prop="ztj">
        <template slot-scope="scope">
          <span>{{ scope.row.ztj }}m³</span>
        </template>
      </el-table-column>
      <el-table-column label="总重量" align="center" prop="zl">
        <template slot-scope="scope">
          <span>{{ scope.row.zl }}kg</span>
        </template>
      </el-table-column>
      <el-table-column label="运费合计" align="center" prop="yfhj" />
      <el-table-column label="下单时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order/order";
import {parseTime} from "../../../utils/ruoyi";
import {countGoods, listGoods} from "@/api/goods/goods";
import * as echarts from 'echarts';
import axios from "axios";

const option = {
  title: {
    text: '订单趋势'
  },
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: [],
      type: 'line',
      smooth: true
    }
  ]
};

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单统计表格数据
      orderList: [],
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 下单时间时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    let chartDom = document.getElementById('map');
    let myChart = echarts.init(chartDom);
    myChart.setOption(option);

    countGoods().then(res => {
      let data = res.rows;
      let xData = [];
      let yData = [];
      for (let i = 0; i < data.length; i++) {
        xData.push(data[i].time);
        yData.push(data[i].num);
      }
      option.xAxis.data = xData;
      option.series[0].data = yData;
      myChart.setOption(option);
    })

    // axios.get('goods/charts').then(response => {
    //   alert(response.data);
    //   let data = response.data;
    //   let xData = [];
    //   let yData = [];
    //   for (let i = 0; i < data.length; i++) {
    //     xData.push(data[i].time);
    //     yData.push(data[i].num);
    //   }
    //   option.xAxis.data = xData;
    //   option.series[0].data = yData;
    //   myChart.setOption(option);
    // })

  },
  methods: {
    parseTime,
    /** 查询订单统计列表 */
    getList() {
      // this.loading = true;
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      // listOrder(this.queryParams).then(response => {
      //   this.orderList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });

      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ddh: null,
        startaddr: null,
        endaddr: null,
        name: null,
        status: null,
        num: null,
        tj: null,
        zl: null,
        hj: null,
        time: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单统计编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
